﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using MyDMS.DAL;
using ListItem = ExtAspNet.ListItem;

namespace MyDMS_ExtJS.Admin
{
    public partial class Folder : System.Web.UI.Page
    {
        private MyDMSEntity _myDmsEntity = new MyDMSEntity();
        protected void Page_Load(object sender, EventArgs e)
        {

            btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");
            btnNew.OnClientClick = Window1.GetShowReference("FolderEdit.aspx", "新增");

            if (!IsPostBack)
            {
                BindTask();
                BindGrid();
            }
        }
        private void BindTask()
        {
            ddlTask.DataSource = from t in _myDmsEntity.Task.ToList()
                                 select new
                                 {
                                     t.Task_Code,
                                     TaskTitle =
                          _myDmsEntity.Project.FirstOrDefault(p => p.Project_Code == t.Project_Code).Project_Title + " - " +
                          t.Task_Title
                                 };
            ddlTask.DataTextField = "TaskTitle";
            ddlTask.DataValueField = "Task_Code";
            ddlTask.DataBind();
            ddlTask.Items.Insert(0, new ListItem("选择小项", "-1"));
        }


        private void BindGrid(int TaskCode = 0)
        {
            if (TaskCode != 0)
            {
                Grid1.DataSource = _myDmsEntity.Folder.Where(t => t.Task_Code == TaskCode).ToList();
            }
            else
            {
                Grid1.DataSource = _myDmsEntity.Folder.ToList();
            }
            Grid1.DataBind();
        }
        protected string GetEditUrl(object id, object name)
        {
            return "javascript:" + Window1.GetShowReference("FolderEdit.aspx?id=" + id, "编辑 - " + name);
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            var n = 0;
            var faultItem = string.Empty;
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                int index = Convert.ToInt32(Grid1.DataKeys[row][0].ToString());
                var sf = _myDmsEntity.Folder.FirstOrDefault(d => d.Folder_Code == index);
                if (sf != null)
                {
                    var t = _myDmsEntity.Folder.FirstOrDefault(tt => tt.Folder_Code == sf.Folder_Code);
                    if (t != null)
                    {
                        faultItem += sf.Folder_Title + ",";
                    }
                    else
                    {
                        n += 1;
                        _myDmsEntity.Folder.DeleteObject(sf);
                    }
                }
                _myDmsEntity.SaveChanges();
            }
            if (n == Grid1.SelectedRowIndexArray.Count())
            {
                Alert.ShowInTop("你删除了" + n + "项");
            }
            else
            {
                Alert.ShowInTop("你删除了" + n + "项\n" + faultItem.Trim(',') + "由于已存在小项，无法删除！");
            }
            BindGrid();
        }

        protected void ddlTask_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid(Convert.ToInt32(ddlTask.SelectedValue));
        }
    }
}